package frame;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import dao.AdminDao;
import model.Admin;







public class AdminPanel extends CommonPanel{
    private Box center = Box.createHorizontalBox();

    //列的集合
    private Vector columns;
    //种类介绍的文本域
    private JTextField ano;

    //种类名称的文本框
    private JTextField aname;

    //隐藏的种类id
    private JTextField id;

    //种类名称的文本框
    private JTextField password;

    //保存的按钮
    private JButton saveButton;

    //删除的按钮
    private JButton deleButton;

    //清空的按钮
    private JButton clearButton;

    //初始化列
    public void initColumns() {
        this.columns = new Vector();
        this.columns.add("id");
        this.columns.add("账号");
        this.columns.add("姓名");
        this.columns.add("密码");
        setViewDatas();
    }

    @Override
    //实现父类的方法, 获得这个界面的列
    public Vector<String> getColumns() {
        return this.columns;
    }

    @Override
    public void setTableFace() {
        // TODO Auto-generated method stub

    }

    public AdminPanel() {

        //设置数据
        setViewDatas();
        //初始化列
        initColumns();
        //设置列表
        DefaultTableModel model = new DefaultTableModel(null, this.columns);
        JTable table = new CommonJTable(model);
        //设置父类的JTable对象
        setJTable(table);
        JScrollPane upPane = new JScrollPane(table);
        upPane.setPreferredSize(new Dimension(1000, 350));

        //设置添加, 修改的界面
        JPanel downPane = new JPanel();
        downPane.setLayout(new BoxLayout(downPane, BoxLayout.Y_AXIS));

        Box downBox1 = new Box(BoxLayout.X_AXIS);
        //存放id的文本框
        id = new JTextField();
        id.setVisible(false);
        downBox1.add(id);
        //列表下面的box
        downBox1.add(new JLabel("账号："));
        downBox1.add(Box.createHorizontalStrut(10));

        ano = new JTextField(10);
        downBox1.add(ano);
        downBox1.add(Box.createHorizontalStrut(20));

        Box downBox2 = new Box(BoxLayout.X_AXIS);
        downBox2.add(new JLabel("姓名："));
        downBox2.add(Box.createHorizontalStrut(10));

        aname = new JTextField(10);
        downBox2.add(aname);
        downBox2.add(Box.createHorizontalStrut(20));

        Box downBox3 = new Box(BoxLayout.X_AXIS);
        downBox3.add(new JLabel("密码："));
        downBox3.add(Box.createHorizontalStrut(10));

        password = new JTextField(10);
        downBox3.add(password);
        downBox3.add(Box.createHorizontalStrut(20));

        /*******************************************************/
        Box downBox4 = new Box(BoxLayout.X_AXIS);

        saveButton = new JButton("保存");
        downBox4.add(saveButton);
        downBox4.add(Box.createHorizontalStrut(30));
        deleButton = new JButton("删除");
        downBox4.add(deleButton);
        downBox4.add(Box.createHorizontalStrut(30));
        clearButton = new JButton("清空");
        downBox4.add(clearButton);
        downBox4.add(Box.createHorizontalStrut(30));

        downPane.add(getSplitBox());
        downPane.add(downBox1);
        downPane.add(getSplitBox());
        downPane.add(downBox2);
        downPane.add(getSplitBox());
        downPane.add(downBox3);
        downPane.add(getSplitBox());
        downPane.add(downBox4);

        //查询的JPanel
        JPanel queryPanel = new JPanel();
        Box queryBox = new Box(BoxLayout.X_AXIS);

        queryBox.add(Box.createHorizontalStrut(30));

        queryBox.add(Box.createHorizontalStrut(30));

        queryPanel.add(queryBox);
        this.add(queryPanel);

        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upPane, downPane);
        split.setDividerSize(5);
        this.add(split);
        //添加监听器
        initListeners();
    }

    //添加监听器
    private void initListeners() {
        //表格选择监听器
        getJTable().getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event) {
                //当选择行时鼠标释放时才执行
                if (!event.getValueIsAdjusting()) {
                    //如果没有选中任何一行, 则返回
                    if (getJTable().getSelectedRowCount() != 1) return;
                    //调用父类的方法获得选择行的id
                    String id = getSelectId(getJTable());
                    System.out.print("id========="+id);
                    view(id);
                }
            }
        });
        //清空按钮监听器
        clearButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                clear();
            }
        });

        //删除按钮监听器
        deleButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                dele();
            }
        });

        //保存按钮监听器
        saveButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                //验证空值
                if (ano.getText().trim().equals("")) {
                    showWarn("请输入账号");
                    return;
                }
                if (aname.getText().trim().equals("")) {
                    showWarn("请输入姓名");
                    return;
                }
                //判断种类id是否有值, 有值则为修改, 没有值为新增
                save();
            }
        });

    }







    //查看一个种类
    public void view(String id) {
        try {
            AdminDao adminDao = new AdminDao();
            Admin admin = adminDao.getData(id);

            this.id.setText(admin.getId());
            ano.setText(admin.getAno());
            aname.setText(admin.getAname());
            password.setText(admin.getPassword());
        }catch(Exception e) {

        }

    }


    @Override
    public void setViewDatas() {
        // TODO Auto-generated method stub
        try {
            AdminDao adminDao = new AdminDao();
            Vector<Admin> admins = (Vector<Admin>)adminDao.getDatas();
            //转换数据格式
            Vector<Vector> datas =  changeDatas(admins);
            //设置数据
            setDatas(datas);
            //刷新列表
            refreshTable();
        }catch(Exception e) {

        }
    }

    /**
     * 将数据转换成视图表格的格式
     * @param datas
     * @return
     */
    private Vector<Vector> changeDatas(Vector<Admin> datas) {
        Vector<Vector> view = new Vector<Vector>();
        for (Admin ss : datas) {
            Vector v = new Vector();
            v.add(ss.getId());
            v.add(ss.getAno());
            v.add(ss.getAname());
            v.add(ss.getPassword());
            view.add(v);
        }
        return view;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub
        refreshTable();
        id.setText("");
        ano.setText("");
        aname.setText("");
        password.setText("");
    }

    public void save() {
        String ano = this.ano.getText();
        String aname = this.aname.getText();
        String password = this.password.getText();
        String id = this.id.getText();
        Admin admin = new Admin();
        admin.setAno(ano);
        admin.setAname(aname);
        admin.setPassword(password);
        admin.setId(id);
        AdminDao adminDao = new AdminDao();
        try {
            if(id==null || id.equals("")) {
                adminDao.insertData(admin);
                JOptionPane.showMessageDialog(center, "保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            }else {
                adminDao.updateData(admin);
                JOptionPane.showMessageDialog(center, "更改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            }
            setViewDatas();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            JOptionPane.showMessageDialog(center, "修改失败", "error", JOptionPane.ERROR_MESSAGE);
            System.out.print("修改数据失败");
        }
    }

    public void dele() {
        String id = this.id.getText();
        if(id==null || id.equals("")) {
            System.out.print("请选择删除的数据");
        }else {
            AdminDao adminDao = new AdminDao();
            try {
                adminDao.deleData(id);
                JOptionPane.showMessageDialog(center, "删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                setViewDatas();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                JOptionPane.showMessageDialog(center, "删除失败", "error", JOptionPane.ERROR_MESSAGE);
                System.out.print("修改删除失败");
            }
        }
    }
}
